<template>
	<div>
		<jk-dialog
			v-model="showDialog"
			:title="`${dataItem.name || '无'}-任务列表`"
			width="900px"
			:show-content-loading="showLoading"
			append-to-body
			:show-confirm-btn="false"
			:show-close-btn="true"
			:show-cancel-btn="false"
			@on-close="onVisibleChange(false)"
			@on-visible-change="onVisibleChange"
		>
			<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
				<!--<el-row ref="box1">
                    <el-col :span="12">
                        <el-form-item class="margin-bottom-10" label="编号" prop="code">
                            <el-input v-model="ruleForm.code" placeholder="编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item class="margin-bottom-10" label="名称" prop="name">
                            <el-input v-model="ruleForm.name" placeholder="名称" />
                        </el-form-item>
                    </el-col>
                </el-row>-->
				<div>
					<el-tag ref="box2" type="primary" effect="plain" class="margin-bottom-4">
						<span class="el-icon-tickets item-gap"></span>
						<span>任务列表</span>
					</el-tag>
					<jk-table
						ref="jkTable"
						border
						:data="tableData"
						:height="dialogTableHeight - 20"
						@checkbox-change="onCheckBoxChange"
					>
						<vxe-column type="checkbox" width="60" align="center"></vxe-column>
						<vxe-column title="品种" align="left" field="productName" :min-width="160"></vxe-column>
						<vxe-column title="批号" align="left" field="batchCode" :min-width="140"></vxe-column>
						<vxe-column title="数量" align="right" field="productionQty" :width="80"></vxe-column>
						<vxe-column title="通知单" align="left" field="prdNoticeCode" :min-width="120"></vxe-column>
						<vxe-column title="生产订单" align="left" field="productOrderCode" :min-width="120"></vxe-column>
						<vxe-column title="生产状态" align="center" field="openingState" :width="90">
							<template slot-scope="scope">
								<jk-state-bar label-key="operationName" :state="scope.row.openingState" :state-list="openStateList"></jk-state-bar>
							</template>
						</vxe-column>
					</jk-table>
					<div ref="box3" class="flex-center margin-top-10">
						<el-button :disabled="!checkArr.length" type="primary" size="small" @click="onBatchOpenBtn">批量开台</el-button>
						<el-button :disabled="!checkArr.length" type="danger" size="small" @click="onBatchCloseBtn">批量了机</el-button>
					</div>
				</div>
			</el-form>
		</jk-dialog>
		<batch-open-dialog
			:machine-list="checkArr"
			:dialog-state="batchOpenDialogState"
			@on-visible-change="onVisibleChangeOfBatchOpenDialog"
			@on-confirm="onConfirmChangeOfBatchOpenDialog"
		></batch-open-dialog>
		<batch-close-dialog
			:machine-list="checkArr"
			:dialog-state="batchCloseDialogState"
			@on-visible-change="onVisibleChangeOfBatchCloseDialog"
			@on-confirm="onConfirmChangeOfBatchCloseDialog"
		></batch-close-dialog>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { openStateList } from '../../../../utils/jk/status-option-list';
    import batchCloseDialog from '../../productOpenCloseMachine/components/batch-close-dialog';
    import batchOpenDialog from '../../productOpenCloseMachine/components/batch-open-dialog';
    import { tipsBar } from '../../../../utils/jk/jk-tool';
    export default {
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataItem: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        components: {
            batchCloseDialog,
            batchOpenDialog
        },
        mixins: [calHeight],
        data() {
            return {
                checkArr: [],
                batchOpenDialogState: false,
                batchCloseDialogState: false,
                openStateList: openStateList,
                showDialog: false,
                showLoading: false,
                ruleForm: {
                    id: null,
                    code: '',
                    name: '',
                    beginHour: '',
                    endHour: '',
                    workingHours: '',
                    typeId: null
                },
                rules: {
                    code: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }],
                    beginHour: [{ required: true, trigger: 'change' }],
                    endHour: [{ required: true, trigger: 'change' }],
                    typeId: [{ required: true, trigger: 'change' }]
                },
                tableData: []
            };
        },
        methods: {
            onBatchCloseBtn() {
                if (Array.from(new Set(this.checkArr.map(x => x.openingState))).length === 1) {
                    this.batchCloseDialogState = true;
                } else {
                    tipsBar(this, 'custom', 'warning', '只能选择待了机的任务!');
                }
            },
            onBatchOpenBtn() {
                if (Array.from(new Set(this.checkArr.map(x => x.openingState))).length <= 2) {
                    if (this.checkArr.some(x => ![1, 2].includes(x.openingState))) {
                        tipsBar(this, 'custom', 'warning', "只能选择'待翻改'或'待开台'的任务!");
                    } else {
                        this.batchOpenDialogState = true;
                    }
                } else {
                    tipsBar(this, 'custom', 'warning', "只能选择'待翻改'或'待开台'的任务!");
                }
            },
            onCheckBoxChange({ records }) {
                this.checkArr = records.filter(x => !x._disabled).map(x => {
                    return x;
                });
            },
            onConfirmChangeOfBatchCloseDialog() {
                const arr = this.checkArr.map(x => x.id);
                this.tableData = this.tableData.filter(x => !arr.includes(x.id));
                this.batchCloseDialogState = false;
                this.$emit('on-finish');
            },
            onConfirmChangeOfBatchOpenDialog() {
                const arr = this.checkArr.map(x => x.id);
                this.tableData = this.tableData.filter(x => !arr.includes(x.id));
                this.batchOpenDialogState = false;
                this.$emit('on-finish');
            },
            onVisibleChangeOfBatchCloseDialog(e) {
                this.batchCloseDialogState = e;
            },
            onVisibleChangeOfBatchOpenDialog(e) {
                this.batchOpenDialogState = e;
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                return params;
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            async getDependentData() {
                this.showLoading = true;
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.tableData = JSON.parse(JSON.stringify(this.dataItem)).taskList;
                    this.getDependentData();
                }
            }
        }
    };
</script>
